package com.liushuang.waiwaiim.controller.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.liushuang.waiwaiim.R;
import com.liushuang.waiwaiim.model.Model;
import com.liushuang.waiwaiim.model.bean.UserInfo;

public class AddContactActivity extends AppCompatActivity {

    private TextView tvAddFind;
    private EditText etAddName;
    private RelativeLayout rlAdd;
//    private ImageView ivAddPhoto;
    private TextView tvAddName;
    private TextView tvAddAdd;
    private UserInfo userInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact);
        
        initView();
        initListener();
        
        
    }

    private void initListener() {
        //为查找按钮设置点击监听事件
        tvAddFind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                find();
            }
        });
        
        //为添加按钮设置点击监听事件
        tvAddAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                add();
            }
        });
    }

    private void add() {
        Model.getInstance().getGlobalExecutorService().execute(new Runnable() {
            @Override
            public void run() {

                //先向环信服务器中添加好友
                try {
                    EMClient.getInstance().contactManager().addContact(userInfo.getName(), "添加好友");
                    
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AddContactActivity.this, "发送添加好友邀请成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(AddContactActivity.this, "发送添加好友邀请失败" + e.toString(), Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }

    private void find() {
        //获取输入的用户名称
        String name = etAddName.getText().toString();
        
        if (TextUtils.isEmpty(name)){
            Toast.makeText(this, "输入的用户名称不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        //去服务器中判断输入框中的用户是否存在
        Model.getInstance().getGlobalExecutorService().execute(new Runnable() {
            @Override
            public void run() {

                // 去服务器判断当前查找的用户是否存在,这里自己没有服务器，默认所有查找的用户均存在
                userInfo = new UserInfo(name);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        rlAdd.setVisibility(View.VISIBLE);
                        tvAddName.setText(userInfo.getName());
                    }
                });

            }
        });
        
    }

    private void initView() {
        tvAddFind = (TextView) findViewById(R.id.tv_add_find);
        etAddName = (EditText) findViewById(R.id.et_add_name);
        rlAdd = (RelativeLayout) findViewById(R.id.rl_add);
//        ivAddPhoto = (ImageView) findViewById(R.id.iv_add_photo);
        tvAddName = (TextView) findViewById(R.id.tv_add_name);
        tvAddAdd = (TextView) findViewById(R.id.tv_add_add);
    }
}